package view.components.button
{
	/**
	 * @author Alejandro
	 */
	
	import flash.events.MouseEvent;
	import flash.media.Sound;
	import flash.net.URLRequest;
	import flash.events.IOErrorEvent;
	import flash.media.SoundTransform;
	import flash.media.SoundChannel
	import view.components.button.AbstractEstructureButton;
	 
	public class CompleteButton extends AbstractEstructureButton{
		private var soundProperty:Sound;
		private var channelProperty:SoundChannel;
		private var transformProperty:SoundTransform;
	
		public function CompleteButton(properties_V:Object) {
			super(properties_V);
			init();
		}
		
		private function init():void {			
			loadSound();
			configurePosition();
			configureSound();
			configureStates();
		}
		
		private function configurePosition():void {
			this.x = properties.x;
			this.y = properties.y;
		}
		
		private function configureSound():void {		
			this.addEventListener(MouseEvent.CLICK, playSound);
			this.addEventListener(MouseEvent.MOUSE_OVER, playSound);
		}
		
		private function loadSound():void{
			soundProperty = new Sound();
			transformProperty = new SoundTransform(0.3, 1.0);
			soundProperty.addEventListener(IOErrorEvent.IO_ERROR, loadSoundErrorHandler);
			soundProperty.load(new URLRequest(properties.soundPath));
		}
		
		private function playSound(clickEvent:MouseEvent):void {
			channelProperty = soundProperty.play();
			channelProperty.soundTransform = transformProperty;			
		}

		private function loadSoundErrorHandler(errorEvent:IOErrorEvent):void {
			trace("El archivo de audio no ha podido ser cargado: " + errorEvent.text);
		}
			
		private function configureStates():void {
			visible        = properties.visible;
			useHandCursor  = properties.useHandCursor;
			downState      = new ButtonDisplayState(properties, downProperties);
			overState      = new ButtonDisplayState(properties, overProperties);
			upState        = new ButtonDisplayState(properties, upProperties);
			hitTestState   = overState;
			//hitTestState   = new ButtonDisplayState(upColor, size * 2);
			//hitTestState.x = -(size / 4);
			//hitTestState.y = hitTestState.x;
		}
	}
	
}

